home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / MATH.SWG / 0088_Staircase Problem.pas < prev    next >
Pascal/Delphi Source File  |  1995-03-03  |  998b  |  40 lines

  1. {
  2. >      Can anyone give me a hand with this problem please! I needs to
  3. > write a recursive function that will compute the number of possible
  4. > ways a person can go up a stair of n steps if that person can take 1,
  5. > 2, or 4 steps in one stride. Thanks in advance!
  6. >
  7. From: R.A.M.vGeel@kub.nl  (GEEL R.A.M.VAN)
  8. }
  9. program StairWay;
  10.  
  11. var
  12.   Total : longint;
  13.   NrOfSteps : integer;
  14.  
  15. procedure ClimbStairs(Steps: integer);
  16.  
  17. begin
  18.   if Steps - 1 = 0 then inc(Total)  { last 1-step }
  19.   else
  20.     if Steps > 0 then
  21.       begin
  22.         if (Steps - 2) = 0 then inc(Total)  { last 2-step }
  23.          else
  24.             if (Steps - 2 > 0) then ClimbStairs(Steps - 2);
  25.  
  26.         if (Steps - 4) = 0 then inc(Total)  { last 4-step }
  27.           else 
  28.             if (Steps - 4) > 0 then ClimbStairs(Steps - 4);  
  29.       end;
  30. end;
  31.  
  32. begin
  33.   Total := 0;
  34.   write('Give number of steps: ');
  35.   readln(NrOfSteps);
  36.   ClimbStairs(NrOfSteps);
  37.   writeln('Total possibilities: ', Total);
  38. end.
  39.  
  40.